package com.twu.bm.football;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

public class SearchOption implements MenuOption {

    private League league;
    private PrintStream printStream;
    private BufferedReader reader;
    private String searchMessage = "To search for a player, enter a name, then enter a number:";

    public SearchOption(PrintStream printStream, BufferedReader reader, League league) {
        this.printStream = printStream;
        this.reader = reader;
        this.league = league;
    }

    public void performAction() {
        startSearch();
    }

    private void startSearch() {
        printStream.println(searchMessage);
        ArrayList<String> nameAndNumber = getTwoUserInputs();

        Player playerFound = league.matchPlayer(nameAndNumber.get(0), nameAndNumber.get(1));

        if (playerFound == null)
            printStream.println("No match found!");
        else
            playerFound.print(printStream);
    }

    private ArrayList<String> getTwoUserInputs() {
        ArrayList<String> inputs = new ArrayList<String>(2);
        inputs.add(null);
        inputs.add(null);

        try {
            inputs.add(0, reader.readLine());
            inputs.add(1, reader.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return inputs;
    }
}
